package com.yeren.thread.Volatile;

/**
 * 2.有序性
 */
public class VolatileDemo1 {
	private  boolean inited=false;
	private  String context="没有初始化化好";
	
	public static void main(String[] args) {
		VolatileDemo1 v=new VolatileDemo1();
		
		Thread t1=new Thread(){
			public void run(){
				System.out.println("线程1启动了");
				v.context=loadContext(v).context;
				v.inited=true;
				System.out.println("线程1结束了");
			}
		};
		
		Thread t2=new Thread(){
			public void run(){
				System.out.println("线程2启动了");
				while(!v.inited ){
					try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
				}
				System.out.println(v.context.toString());
				System.out.println("线程2结束了");
			}
		};
		
		t1.start();
		t2.start();
		
	}
	
	public static VolatileDemo1 loadContext(VolatileDemo1 v){
		try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
		v.context="初始化好了";
		try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
		return v;
	}
	
}

